package com.linqi.freebie.components


import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withLink
import androidx.compose.ui.unit.dp
import com.linqi.freebie.config.Constants.documentwithLink

/**
 * 用户交互区域
 *
 */

@Composable
fun Control(
    appId: String,
    onAppIdChange: (String) -> Unit,
    onStartClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    // 构建"使用说明"带链接的文字
    val annotatedString = buildAnnotatedString {
        withLink(LinkAnnotation.Url(url=documentwithLink)) {
            append("使用说明")
        }
    }

    Box(
        modifier = modifier
            .fillMaxHeight()
            .padding(16.dp),
        contentAlignment = Alignment.TopStart
    ) {
        Column(
            modifier = Modifier.fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // 输入框
            OutlinedTextField(
                value = appId,
                onValueChange = onAppIdChange,
                label = { Text("请输入APP_ID", color = Color(0xFFADFF2F).copy(alpha = 0.8f)) },
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
                modifier = Modifier.fillMaxWidth(),
                singleLine = true,
                maxLines = 1,
                colors = TextFieldDefaults.colors(
                    focusedContainerColor = Color.Transparent,
                    unfocusedContainerColor = Color.Transparent,
                    focusedTextColor = Color.Green.copy(alpha = 0.5f),
                    unfocusedTextColor = Color.Green.copy(alpha = 0.5f),
                    cursorColor = Color.Blue,
                    focusedIndicatorColor = Color.Yellow.copy(alpha = 0.5f),
                    unfocusedIndicatorColor = Color.Yellow.copy(alpha = 0.2f),
                    focusedLabelColor = Color.Green,
                    unfocusedLabelColor = Color.Green
                )
            )

            Spacer(modifier = Modifier.height(16.dp))

            // 提交按钮
            Button(
                onClick = onStartClick,
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(0xFF548B54),
                    contentColor = Color.White
                ),
                modifier = Modifier
                    .align(Alignment.CenterHorizontally)
                    .padding(horizontal = 32.dp)
            ) {
                Text("开始")
            }

            Spacer(modifier = Modifier.weight(1f))

            // 使用说明超链接（底部）
            BasicText(
                text = annotatedString,
                style = TextStyle(
                    color = Color.Yellow,
                    textDecoration = TextDecoration.Underline,
                    fontSize = MaterialTheme.typography.bodyMedium.fontSize
                ),
                modifier = Modifier.padding(bottom = 8.dp)
            )
        }
    }
}